package com.itheima.controller;


import com.itheima.anno.LogOperation;
import com.itheima.pojo.*;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emps")
@Slf4j
public class EmpController {

    @Autowired
    private EmpService empService;


    /**
     * 修改员工
     * @param emp
     * @return
     */
    @LogOperation
    @PutMapping
    public Result updateById(@RequestBody Emp emp){
        empService.updateById(emp);
        return Result.success();
    }


    /**
     * 根据ID查询员工数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据ID查询员工数据:{}",id);
        Emp emp =  empService.getInfo(id);
        return Result.success(emp);
    }


    /****
     * 删除员工
     * @param ids
     * @return
     */
    @LogOperation
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        log.info("删除员工，ids：{}",ids);
        empService.delete(ids);
        return Result.success();
    }


    /**
     * 查询所有员工
     * @return
     */
    @GetMapping("/list")
    public Result findAll(){
        List<Emp> empList = empService.findAll();
        return Result.success(empList);
    }



    /**
     * 添加员工
     * @param emp
     * @return
     */
    @LogOperation
    @PostMapping
    public Result saveEmp(@RequestBody Emp emp){
        empService.saveEmp(emp);
        return Result.success();
    }

    /**
     * 分页查询列表数据
     * @param empQueryParam
     * @return
     */
    @GetMapping
    public Result page(EmpQueryParam empQueryParam) {
        log.info("分页查询：{}",empQueryParam);
        PageResult pageResult = empService.page(empQueryParam);
        return Result.success(pageResult);
    }
}
